package com.fangyi.yunmusic.service.impl;

import com.fangyi.yunmusic.config.ResourceConfig;
import com.fangyi.yunmusic.exception.YunMusicException;
import com.fangyi.yunmusic.mapper.PlaylistHasTagMapper;
import com.fangyi.yunmusic.mapper.PlaylistMapper;
import com.fangyi.yunmusic.mapper.PlaylistTagMapper;
import com.fangyi.yunmusic.mapper.UserMapper;
import com.fangyi.yunmusic.pojo.Playlist;
import com.fangyi.yunmusic.pojo.PlaylistHasTag;
import com.fangyi.yunmusic.pojo.PlaylistTag;
import com.fangyi.yunmusic.pojo.User;
import com.fangyi.yunmusic.pojo.vo.PlaylistVO;
import com.fangyi.yunmusic.pojo.vo.TagVO;
import com.fangyi.yunmusic.pojo.vo.UserVO;
import com.fangyi.yunmusic.service.PlaylistTagService;
import com.fangyi.yunmusic.utils.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.n3r.idworker.Id;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

@Service
public class PlaylistTagServiceImpl implements PlaylistTagService {
    @Autowired
    private PlaylistMapper playlistMapper;
    @Autowired
    private PlaylistHasTagMapper playlistHasTagMapper;
    @Autowired
    private PlaylistTagMapper playlistTagMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ResourceConfig resourceConfig;
    @Override
    public void addPlaylistTag(String tagName, String playlistId) {
        if(playlistMapper.selectByPrimaryKey(playlistId)==null)
            throw new YunMusicException("歌单ID不存在！");
        Example example=new Example(PlaylistTag.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("name",tagName);

        List<PlaylistTag> playlistTags=playlistTagMapper.selectByExample(example);
        if(playlistTags.size()==1)
            throw new YunMusicException("歌单标签名称已存在");
        String tagId=Id.nextString();
        PlaylistTag playlistTag=new PlaylistTag();
        playlistTag.setId(tagId);
        playlistTag.setName(tagName);
        playlistTagMapper.insert(playlistTag);
        String playlistHasTagId=Id.nextString();
        PlaylistHasTag playlistHasTag=new PlaylistHasTag();
        playlistHasTag.setId(playlistHasTagId);
        playlistHasTag.setPalylistTagId(tagId);
        playlistHasTag.setPlaylistId(playlistId);
        playlistHasTagMapper.insert(playlistHasTag);
    }

    @Override
    public void removePlaylistTag(String tagId) {
        if(playlistTagMapper.selectByPrimaryKey(tagId)==null)
            throw new YunMusicException("歌单标签ID不存在");
        Example example=new Example(PlaylistHasTag.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("palylistTagId",tagId);
        playlistHasTagMapper.deleteByExample(example);
        playlistTagMapper.deleteByPrimaryKey(tagId);
    }

    @Override
    public List<TagVO> getAllPlaylistTagVOs() {
        return   playlistTagMapper.selectAll().stream().map(PlaylistTag->{
            TagVO tagVO=new TagVO();
            BeanUtils.copyProperties(PlaylistTag,tagVO);
            return  tagVO;

        }).collect(toList());
    }

    @Override
    public PagedResult getPlaylistVOsByTagId(String tagId, Integer page, Integer pageSize) {
        if(tagId!=null&&playlistTagMapper.selectByPrimaryKey(tagId)==null)
            throw new YunMusicException("歌单标签ID不存在");
        Example example=new Example(PlaylistHasTag.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("palylistTagId",tagId);
        List<String> playlistIds= playlistHasTagMapper.selectByExample(example).stream().map(PlaylistHasTag::getPlaylistId).collect(toList());
        Example example1=new Example(Playlist.class);
        Example.Criteria criteria1=example1.createCriteria();
        criteria1.andIn("id",playlistIds);
        PageHelper.startPage(page,pageSize);
        List<Playlist> playlists=playlistMapper.selectByExample(example1);
        List<PlaylistVO> playlistVOS=playlists.stream().map(Playlist->{
            PlaylistVO playlistVO=new PlaylistVO();
            BeanUtils.copyProperties(Playlist,playlistVO);
            playlistVO.setCoverImgUrl(resourceConfig.getPlaylistCoverUrl(playlistVO.getCoverImgId()));
            UserVO userVO = new UserVO();
            User user=userMapper.selectByPrimaryKey(playlistVO.getUserId());
            BeanUtils.copyProperties(user, userVO);
            userVO.setAvatarImgUrl(resourceConfig.getUserAvatarUrl(user.getAvatarImgId()));
            playlistVO.setUserVO(userVO);
            return playlistVO;


        }).collect(toList());
        PageInfo<?> pageInfo = new PageInfo<>(playlists);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return new PagedResult(playlistVOS,page,pages,total);
    }
}
